package com.example.webdownloadserver.base;

import com.example.webdownloadserver.common.TaskConstants;
import com.example.webdownloadserver.constants.TaskCenterConstants;
import com.example.webdownloadserver.service.TaskCenterService;
import com.example.webdownloadserver.util.TxtFileParseUtil;
import com.example.webdownloadserver.vo.TaskCenter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


@Slf4j
public abstract class TaskThreadAbastract<P> {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	protected TaskCenterService taskCenterService;
	
	/**
	 * 写结果文件
	 * @param succ			成功数量
	 * @param paramList		参数集合
	 * @param repeatList	重读集合
	 * @param failList		失败集合
	 * @param errData		错误信息
	 * @param downpath		下载路径
	 * @param downfilename	下载文件名
	 * @param title			标题
	 * @param paramName		参数名
	 * @throws IOException
	 */
	protected void wirteFile(Integer succ,Collection<?> paramList,List<String> repeatList,List<String> failList,StringBuffer errData,String downpath,String downfilename,String title,String paramName) throws IOException{
    	List<String> temp = new ArrayList<String>();
    	if(succ == paramList.size()) {
    		temp.add(title+"全部成功，数量:"+succ);
    	}else {
    		if(succ == 0) {
    			temp.add(title+"全部失败，数量:"+paramList.size());
    		}else {
    			temp.add(title+"共"+paramList.size()+"个"+paramName+"，其中成功数量:"+succ);
    		}
    		if(CollectionUtils.isNotEmpty(repeatList)) {
    			temp.add("重复"+paramName+"共："+repeatList.size()+"个--"+repeatList.toString());
    		}
    		if(CollectionUtils.isNotEmpty(failList)) {
    			temp.add("失败"+paramName+"共："+failList.size()+"个--"+failList.toString());
    		}
    		String[] reData = errData.toString().split("//");
    		if (reData.length > 0 && StringUtils.isNotBlank(reData[0]))
			{
				for (int i = 0; i < reData.length; i++)
				{
					temp.add(reData[i]);
				}
			}
    	}
    	TxtFileParseUtil.writeFile(downpath, downfilename, temp);
	}

	protected void wirteFile(List<String> list, String fileName, int total, TaskCenter taskCenter){
		//回写回执文件
		try {
			int sucNum = total - list.size();
			list.add("总数为：" + total + "成功：" + sucNum + "失败：" + list.size());
			TxtFileParseUtil.writeFile(TaskConstants.UPLOADPATH,fileName,list);
			taskCenter.setTaskDownUrl("opt/upload"+fileName);
			taskCenter.setTaskStatus(TaskCenterConstants.TASK_STATUS_SUCC);
			taskCenterService.update(taskCenter);
		} catch (IOException e) {
			log.error("回写回执文件失败！");
			e.printStackTrace();
			taskCenter.setTaskStatus(TaskCenterConstants.TASK_STATUS_FAIL);
			taskCenter.setTaskRemark(TaskCenterConstants.UPLOAD_DO_ERROR);
			taskCenterService.update(taskCenter);
		}

	}

	protected void wirteFile(int succ,int total,List<String> repeatList,List<String> failList,StringBuffer errData,String downpath,String downfilename,String title,String paramName) throws IOException{
    	List<String> temp = new ArrayList<String>();
    	if(succ == total) {
    		temp.add(title+"全部成功，数量:"+succ);
    	}else {
    		if(succ == 0) {
    			temp.add(title+"全部失败，数量:"+total);
    		}else {
    			temp.add(title+"共"+total+"个"+paramName+"，其中成功数量:"+succ);
    		}
    		if(CollectionUtils.isNotEmpty(repeatList)) {
    			temp.add("重复"+paramName+"共："+repeatList.size()+"个--"+repeatList.toString());
    		}
    		if(CollectionUtils.isNotEmpty(failList)) {
    			temp.add("失败"+paramName+"共："+failList.size()+"个--"+failList.toString());
    		}
    		String[] reData = errData.toString().split("//");
    		if (reData.length > 0 && StringUtils.isNotBlank(reData[0]))
			{
				for (int i = 0; i < reData.length; i++)
				{
					temp.add(reData[i]);
				}
			}
    	}
    	TxtFileParseUtil.writeFile(downpath, downfilename, temp);
	}

	public abstract void doing(List<P> datalist, TaskCenter taskCenter) throws ParseException;
}
